package org.dromara.chicken.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.chicken.domain.ChickenHouse;
import org.dromara.chicken.service.IChickenHouseService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 鸡舍管理
 *
 * @author Mr Lei
 * @date 2024-06-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/farm/house")
public class ChickenHouseController extends BaseController {

    private final IChickenHouseService chickenHouseService;

    /**
     * 查询鸡舍管理列表
     */
    @SaCheckPermission("chicken:house:list")
    @GetMapping("/list")
    public TableDataInfo<ChickenHouse> list(ChickenHouse bo, PageQuery pageQuery) {
        return chickenHouseService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出鸡舍管理列表
     */
    @SaCheckPermission("chicken:house:export")
    @Log(title = "鸡舍管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ChickenHouse bo, HttpServletResponse response) {
        List<ChickenHouse> list = chickenHouseService.queryList(bo);
        ExcelUtil.exportExcel(list, "鸡舍管理", ChickenHouse.class, response);
    }

    /**
     * 获取鸡舍管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("chicken:house:query")
    @GetMapping("/{id}")
    public R<ChickenHouse> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(chickenHouseService.queryById(id));
    }

    /**
     * 新增鸡舍管理
     */
    @SaCheckPermission("chicken:house:add")
    @Log(title = "鸡舍管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ChickenHouse bo) {
        return toAjax(chickenHouseService.insertByBo(bo));
    }

    /**
     * 修改鸡舍管理
     */
    @SaCheckPermission("chicken:house:edit")
    @Log(title = "鸡舍管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ChickenHouse bo) {
        return toAjax(chickenHouseService.updateByBo(bo));
    }

    /**
     * 删除鸡舍管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("chicken:house:remove")
    @Log(title = "鸡舍管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(chickenHouseService.deleteWithValidByIds(List.of(ids), true));
    }
}
